package com.example.rabbitmq6.config;

import com.example.rabbitmq6.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {
    @Bean("retryExchange")
    public DirectExchange retryExchange() {
        return ExchangeBuilder.directExchange(Constants.RETRY_EXCHANGE).durable(true).build();
    }

    @Bean("retryQueue")
    public Queue retryQueue() {
        return QueueBuilder.durable(Constants.RETRY_QUEUE).build();
    }

    @Bean("retryBinding")
    public Binding retryBinding(@Qualifier("retryQueue") Queue queue, @Qualifier("retryExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("retry");
    }

    @Bean("ttlExchange")
    public DirectExchange ttlExchange() {
        return ExchangeBuilder.directExchange(Constants.TTL_EXCHANGE).durable(true).build();
    }

    @Bean("ttlQueue")
    public Queue ttlQueue() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl",20000);
//        return QueueBuilder.durable(Constants.TTL_QUEUE).ttl(20*1000).build();
//        return QueueBuilder.durable(Constants.TTL_QUEUE).withArguments(arguments).build();
        return QueueBuilder.durable(Constants.TTL_QUEUE).withArgument("x-message-ttl",20000).build();
    }

//    @Bean("normalQueue")
//    public Queue normalQueue() {
//        return QueueBuilder.durable(Constants.NORMAL_QUEUE).build();
//    }

    @Bean("ttlBinding")
    public Binding ttlBinding(@Qualifier("ttlQueue") Queue queue,@Qualifier("ttlExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ttl");
    }

    @Bean("ttlBinding2")
    public Binding ttlBinding2(@Qualifier("normalQueue") Queue queue,@Qualifier("ttlExchange") DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("ttl");
    }
}
